<?php
if(! defined('BASEPATH')) {
	exit('No direct script access allowed');
}

/**
 *团队模块操作
 *@author 王春林
 */
class team_model extends CI_Model {
	function __construct() {
		parent::__construct();
	}
	/**
	 *判断团队名称是否存在
	 *@param $teamName
	 *@return int
	 */
	function isuse($teamName) {
		$this->db->where('teamName', $teamName );
		$rs = $this->db->get('wanwan_enum_team' )->result();
		return $rs->num_rows ();
	}
	/**
	 *创建团队
	 *@param $team Array
	 */
	function createTeam($team) {
		$this->db->insert('wanwan_enum_team',$team);
		//返回插入数据的ID
		$id = $this->db->insert_id();
		$userId = $this->session->userdata("userId");
		$date = date("Y-m-d H:i:s");
		$teamName = $team['teamNickname'];
		$sql = "insert into wanwan_enum_usertoteam(userId,teamId,teamName,createTime,isCreator) values('$userId','$id','$teamName','$date',1)";
		$this->db->query($sql);
		return $id;
	}
	/**
	 *根据团队ID删除团队
	 *@param $teamId
	 */
	function deleteTeamById($teamId) {
		$this->db->where('teamId',$teamId);
		$this->db->delete('wanwan_enum_team');
	}
	/**
	 *根据团队ID查询团队
	 *@param $teamId
	 *@return Object
	 */
	function queryTeamByTeamId($teamId) {
		$sql = "select * from wanwan_enum_team where teamId = $teamId";
		$rs = $this->db->query($sql);
		return $rs->row_array();
	}
	/**
	 *根据团队所有人ID查询团队
	 *@param $teamId
	 */
	function queryTeamByUserId($userId) {
		$this->db->where('userGid',$userId);
		$rs = $this->db->get('wanwan_enum_team');
		return $rs->result();
	}
	/**
	 *编辑团队
	 *@param $teamId
	 */
	function updateTeam($team) {
		$this->db->where('teamId',$team['teamId']);
		$this->db->update('wanwan_enum_team',$team);
	}
	/**
	 *删除团队并删除该团队下的所有用户
	 *@param $teamId
	 */
	function deleteTeam($teamId) {
		$this->db->where('teamId',$teamId);
		$this->db->delete('wanwan_enum_team');
		$sql = "delete from wanwan_enum_usertoteam where teamId = $teamId";
		$this->db->query($sql);
	}
	/**
	 *删除团队中的某个人
	 *@param $teamId
	 *@param $userId
	 */
	function deleteUserFromTeam($teamId,$userId) {
		$sql = "delete from wanwan_enum_usertoteam where $teamId = $teamId and userId = $userId";
		$this->db->query($sql);
	}
	/**
	 *申请加入团队
	 *@param $teamId int
	 *@param $loginId int
	 */
	function applyJoinToTeam($loginId,$teamId,$teamName) {
		$sql = "insert into wanwan_enum_applyjointoteam(userId,teamId,Status) values($loginId,$teamId,'申请加入')";
		$this->db->query($sql);
		
		$date = date('Y-m-d H:i:s');
		$sql2 = "insert into wanwan_enum_usertoteam(userId,teamId,teamName,createTime,isCreator) values('$loginId','$teamId','$teamName','$date',0)";
		$this->db->query($sql2);
	}
	/**
	 *查询所有申请加入团队的人员名单
	 *@param $teamId
	 */
	function listApplyUsers($teamId) {
		$sql = "select * from wanwan_enum_applyjointoteam where teamId = $teamId and status = '申请加入'";
		$results = $this->db->query($sql)->result();
		return $results;
	}
	/**
	 *审核申请加入团队的成员
	 *@param $id
	 *@param $status
	 */
	function checkUser($id,$status) {
		$sql = "update wanwan_enum_applyjointoteam set status = $status where id = $id";
		$this->db->query($sql);
	}	
	/**
	 *查询某团队下的所有成员
	 *@param $teamId
	 */
	function listUsersByTeamId($teamId) {
		$sql = "select u.*,case when u.userBigIcon is NULL then 'images/default/default.gif' else u.userBigIcon end AS headIcon from wanwan_enum_user u where u.userId in (select userId from wanwan_enum_usertoteam where teamId = $teamId)";
		$rs = $this->db->query($sql);
		return $rs->result_array();
	}
	/**
	 *创建团队公告
	 *@param $notice $notice中包含teamId
	 */
	function createNoticeByTeamId($notice) {
		$this->db->insert("wanwan_enum_notice",$notice);
	}
	/**
	 *看好
	 *@param $teamId
	 */
	function optimistic($teamId) {	
		$rs = $this->queryTeamByTeamId($teamId);
		$rs['optimistic'] = $rs['optimistic']+1;
		$this->db->where('teamId',$teamId);
		$this->db->update("wanwan_enum_team",$rs);
	}
	/**
	 *在意
	 *@param $teamId
	 */
	function care($teamId) {
		$rs = $this->queryTeamByTeamId($teamId);
		$rs['care'] = $rs['care']+1;
		$this->db->where('teamId',$teamId);
		$this->db->update("wanwan_enum_team",$rs);
	}
	/**
	 *查询记录条数
	 *@param $teamId
	 */
	function countUsersDynamic($teamId,$type) {
		$sql = "SELECT count(*) as cont from wanwan_enum_userdynamic ud left join wanwan_enum_user u on ud.userId=u.userId WHERE ud.userId IN (SELECT userId FROM wanwan_enum_usertoteam WHERE teamId = $teamId) AND type = '$type'";
		return $this->db->query($sql)->result();
	}
	/**
	 *查看好友动态,根据teamId 查询所有用户发表的动态消息
	 *@param $teamId
	 */
	function lookUsersDynamic($teamId,$type,$pageNo) {
		log_message("info","pageNo=>".$pageNo);
		$pageSize=2;
		$pageNo=($pageNo-1)*$pageSize;
		$sql = "SELECT ud.*,u.nickName ,case when u.userBigIcon is NULL then 'images/default/default.gif' else u.userBigIcon end AS userBigIocn from wanwan_enum_userdynamic ud left join wanwan_enum_user u on ud.userId=u.userId WHERE ud.userId IN (SELECT userId FROM wanwan_enum_usertoteam WHERE teamId = $teamId) AND type = '$type' limit $pageNo,$pageSize";
		return $this->db->query($sql)->result_array();	
	}
	/**
	 *根据用户动态ID查询动态
	 *@param $dynamicId
	 *@return Object
	 */
	function queryDynamicByDynamicId($dynamicId) {
		$sql = "select * from wanwan_enum_userdynamic where id = $dynamicId";
		log_message("info","log_sql=>".$sql);
		$rs = $this->db->query($sql);
		return $rs->row_array();
	}
	/**
	 *回复用户动态
	 *@param $teamId
	 */
	function replayUsersDynamic($data) {	
		$this->db->insert("wanwan_enum_replyusersdynamic",$data);
		$rs = $this->queryDynamicByDynamicId($data['dynamicId']);
		$rs['replyCount'] = $rs['replyCount']+1;
		log_message("info","log_rs=>".$rs['replyCount']);
		$this->db->where("id",$data['dynamicId']);
		$this->db->update("wanwan_enum_userdynamic",$rs);
	}
	/**
	 *转发用户动态
	 *@param $loginId,$dynamicId
	 */
	function forward($loginId,$dynamicId) {
		$this->db->where("id",$dynamicId);
		$rs = $this->db->get("wanwan_enum_userdynamic")->row_array();
		$rs['userId'] = $loginId;
		unset($rs['id']);
		$this->db->insert("wanwan_enum_userdynamic",$rs);
	}
	/**
	 *根据用户动态ID查看动态内容
	 *@param $dynamicId
	 */
	function queryUserDynamicById($dynamicId) {
		$sql = "SELECT ud.*,us.nickName,CASE WHEN us.userBigIcon IS NULL THEN 'images/default/default.gif' ELSE us.userBigIcon END AS headIcon FROM wanwan_enum_userdynamic ud LEFT JOIN wanwan_enum_user us ON ud.userId = us.userId WHERE ud.id = $dynamicId";
		$rs = $this->db->query($sql);
		return $rs->row_array();
	}
	/**
	 *根据用户动态ID查询动态回复
	 *@param dynamicId
	 */
	function queryDynamicReplyById($dynamicId,$type='dynamic',$parentsId=0) {
		$sql = "SELECT rd.*,us.nickName,CASE WHEN us.userBigIcon IS NULL THEN 'images/default/default.gif' ELSE us.userBigIcon END AS headIcon FROM wanwan_enum_replyusersdynamic rd LEFT JOIN wanwan_enum_user us ON rd.reply_userId = us.userId WHERE rd.type='$type' and rd.parentsId=$parentsId and dynamicId = $dynamicId";
		$rs = $this->db->query($sql);
		return $rs->result_array();
	}
	/**
	 *更新公告
	 *@param $teamId,$content
	 */
	function updateDynamicNotice($content,$teamId) {
		$sql = "UPDATE wanwan_enum_team SET teamNotice='$content' WHERE teamId = $teamId";
		$this->db->query($sql);
	}
	/**
	 *更新简介
	 *@param $teamId,$content
	 */
	function updateDynamicDesc($content,$teamId) {
		$sql = "UPDATE wanwan_enum_team SET teamDescription='$content' WHERE teamId = $teamId";
		$this->db->query($sql);
	}
}
?>